<?php
class ControllerConfigureOption extends Controller {
    private $error = array();

    public function index() {
        $this->language->load('configure/option');

        $this->document->setTitle($this->language->get('heading_title'));

        $this->load->model('configure/option');

        $this->getList();
    }

    public function insert() {
        $this->language->load('configure/option');

        $this->document->setTitle($this->language->get('heading_title'));

        $this->load->model('configure/option');

        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
            $this->model_configure_option->addOption($this->request->post);

            $this->session->data['success'] = $this->language->get('text_success');

            $url = '';

            if (isset($this->request->get['sort'])) {
                $url .= '&sort=' . $this->request->get['sort'];
            }

            if (isset($this->request->get['order'])) {
                $url .= '&order=' . $this->request->get['order'];
            }

            if (isset($this->request->get['page'])) {
                $url .= '&page=' . $this->request->get['page'];
            }

            $this->redirect($this->url->link('configure/option', 'token=' . $this->session->data['token'] . $url, 'SSL'));
        }

        $this->getForm();
    }

    public function update() {
        $this->language->load('configure/option');

        $this->document->setTitle($this->language->get('heading_title'));

        $this->load->model('configure/option');

        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
            $this->model_configure_option->editOption($this->request->get['configureoption_id'], $this->request->post);

            $this->session->data['success'] = $this->language->get('text_success');

            $url = '';

            if (isset($this->request->get['sort'])) {
                $url .= '&sort=' . $this->request->get['sort'];
            }

            if (isset($this->request->get['order'])) {
                $url .= '&order=' . $this->request->get['order'];
            }

            if (isset($this->request->get['page'])) {
                $url .= '&page=' . $this->request->get['page'];
            }

            $this->redirect($this->url->link('configure/option', 'token=' . $this->session->data['token'] . $url, 'SSL'));
        }

        $this->getForm();
    }

    public function delete() {
        $this->language->load('configure/option');

        $this->document->setTitle($this->language->get('heading_title'));

        $this->load->model('configure/option');

        if (isset($this->request->post['selected']) && $this->validateDelete()) {
            foreach ($this->request->post['selected'] as $configureoption_id) {
                $this->model_configure_option->deleteOption($configureoption_id);
            }

            $this->session->data['success'] = $this->language->get('text_success');

            $url = '';

            if (isset($this->request->get['sort'])) {
                $url .= '&sort=' . $this->request->get['sort'];
            }

            if (isset($this->request->get['order'])) {
                $url .= '&order=' . $this->request->get['order'];
            }

            if (isset($this->request->get['page'])) {
                $url .= '&page=' . $this->request->get['page'];
            }

            $this->redirect($this->url->link('configure/option', 'token=' . $this->session->data['token'] . $url, 'SSL'));
        }

        $this->getList();
    }

    protected function getList() {
        if (isset($this->request->get['sort'])) {
            $sort = $this->request->get['sort'];
        } else {
            $sort = 'od.name';
        }

        if (isset($this->request->get['order'])) {
            $order = $this->request->get['order'];
        } else {
            $order = 'ASC';
        }

        if (isset($this->request->get['page'])) {
            $page = $this->request->get['page'];
        } else {
            $page = 1;
        }

        $url = '';

        if (isset($this->request->get['sort'])) {
            $url .= '&sort=' . $this->request->get['sort'];
        }

        if (isset($this->request->get['order'])) {
            $url .= '&order=' . $this->request->get['order'];
        }

        if (isset($this->request->get['page'])) {
            $url .= '&page=' . $this->request->get['page'];
        }

        $this->data['breadcrumbs'] = array();

        $this->data['breadcrumbs'][] = array(
            'text'      => $this->language->get('text_home'),
            'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
            'separator' => false
        );

        $this->data['breadcrumbs'][] = array(
            'text'      => $this->language->get('heading_title'),
            'href'      => $this->url->link('configure/option', 'token=' . $this->session->data['token'] . $url, 'SSL'),
            'separator' => ' :: '
        );

        $this->data['insert'] = $this->url->link('configure/option/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
        $this->data['delete'] = $this->url->link('configure/option/delete', 'token=' . $this->session->data['token'] . $url, 'SSL');

        $this->data['options'] = array();

        $data = array(
            'sort'  => $sort,
            'order' => $order,
            'start' => ($page - 1) * $this->config->get('config_admin_limit'),
            'limit' => $this->config->get('config_admin_limit')
        );

        $option_total = $this->model_configure_option->getTotalOptions();

        $results = $this->model_configure_option->getOptions($data);

        foreach ($results as $result) {
            $action = array();

            $action[] = array(
                'text' => $this->language->get('text_edit'),
                'href' => $this->url->link('configure/option/update', 'token=' . $this->session->data['token'] . '&configureoption_id=' . $result['configureoption_id'] . $url, 'SSL')
            );

            $this->data['options'][] = array(
                'configureoption_id'  => $result['configureoption_id'],
                'name'       => $result['name'],
                'sort_order' => $result['sort_order'],
                'selected'   => isset($this->request->post['selected']) && in_array($result['configureoption_id'], $this->request->post['selected']),
                'action'     => $action
            );
        }

        $this->data['heading_title'] = $this->language->get('heading_title');

        $this->data['text_no_results'] = $this->language->get('text_no_results');

        $this->data['column_name'] = $this->language->get('column_name');
        $this->data['column_sort_order'] = $this->language->get('column_sort_order');
        $this->data['column_action'] = $this->language->get('column_action');

        $this->data['button_insert'] = $this->language->get('button_insert');
        $this->data['button_delete'] = $this->language->get('button_delete');

        if (isset($this->error['warning'])) {
            $this->data['error_warning'] = $this->error['warning'];
        } else {
            $this->data['error_warning'] = '';
        }

        if (isset($this->session->data['success'])) {
            $this->data['success'] = $this->session->data['success'];

            unset($this->session->data['success']);
        } else {
            $this->data['success'] = '';
        }

        $url = '';

        if ($order == 'ASC') {
            $url .= '&order=DESC';
        } else {
            $url .= '&order=ASC';
        }

        if (isset($this->request->get['page'])) {
            $url .= '&page=' . $this->request->get['page'];
        }

        $this->data['sort_name'] = $this->url->link('configure/option', 'token=' . $this->session->data['token'] . '&sort=od.name' . $url, 'SSL');
        $this->data['sort_sort_order'] = $this->url->link('configure/option', 'token=' . $this->session->data['token'] . '&sort=o.sort_order' . $url, 'SSL');

        $url = '';

        if (isset($this->request->get['sort'])) {
            $url .= '&sort=' . $this->request->get['sort'];
        }

        if (isset($this->request->get['order'])) {
            $url .= '&order=' . $this->request->get['order'];
        }

        $pagination = new Pagination();
        $pagination->total = $option_total;
        $pagination->page = $page;
        $pagination->limit = $this->config->get('config_admin_limit');
        $pagination->text = $this->language->get('text_pagination');
        $pagination->url = $this->url->link('configure/option', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');

        $this->data['pagination'] = $pagination->render();

        $this->data['sort'] = $sort;
        $this->data['order'] = $order;

        $this->template = 'configure/option_list.tpl';
        $this->children = array(
            'common/header',
            'common/footer'
        );

        $this->response->setOutput($this->render());
    }

    protected function getForm() {
        $this->data['heading_title'] = $this->language->get('heading_title');

        $this->data['text_choose'] = $this->language->get('text_choose');
        $this->data['text_select'] = $this->language->get('text_select');
        $this->data['text_radio'] = $this->language->get('text_radio');
        $this->data['text_checkbox'] = $this->language->get('text_checkbox');
        $this->data['text_password'] = $this->language->get('text_password');
        $this->data['text_image'] = $this->language->get('text_image');
        $this->data['text_input'] = $this->language->get('text_input');
        $this->data['text_text'] = $this->language->get('text_text');
        $this->data['text_textarea'] = $this->language->get('text_textarea');
        $this->data['text_file'] = $this->language->get('text_file');
        $this->data['text_date'] = $this->language->get('text_date');
        $this->data['text_datetime'] = $this->language->get('text_datetime');
        $this->data['text_time'] = $this->language->get('text_time');
        $this->data['text_image_manager'] = $this->language->get('text_image_manager');
        $this->data['text_browse'] = $this->language->get('text_browse');
        $this->data['text_clear'] = $this->language->get('text_clear');

        $this->data['entry_name'] = $this->language->get('entry_name');
        $this->data['entry_field_name'] = $this->language->get('entry_field_name');
        $this->data['entry_type'] = $this->language->get('entry_type');
        $this->data['entry_option_value'] = $this->language->get('entry_option_value');
        $this->data['entry_field_value'] = $this->language->get('entry_field_value');
        $this->data['entry_sort_order'] = $this->language->get('entry_sort_order');

        $this->data['button_save'] = $this->language->get('button_save');
        $this->data['button_cancel'] = $this->language->get('button_cancel');
        $this->data['button_add_option_value'] = $this->language->get('button_add_option_value');
        $this->data['button_remove'] = $this->language->get('button_remove');

        if (isset($this->error['warning'])) {
            $this->data['error_warning'] = $this->error['warning'];
        } else {
            $this->data['error_warning'] = '';
        }

        if (isset($this->error['name'])) {
            $this->data['error_name'] = $this->error['name'];
        } else {
            $this->data['error_name'] = array();
        }

        if (isset($this->error['field_name'])) {
            $this->data['error_field_name'] = $this->error['field_name'];
        } else {
            $this->data['error_field_name'] = array();
        }

        if (isset($this->error['configureoption_value'])) {
            $this->data['error_option_value'] = $this->error['configureoption_value'];
        } else {
            $this->data['error_option_value'] = array();
        }

        $url = '';

        if (isset($this->request->get['sort'])) {
            $url .= '&sort=' . $this->request->get['sort'];
        }

        if (isset($this->request->get['order'])) {
            $url .= '&order=' . $this->request->get['order'];
        }

        if (isset($this->request->get['page'])) {
            $url .= '&page=' . $this->request->get['page'];
        }

        $this->data['breadcrumbs'] = array();

        $this->data['breadcrumbs'][] = array(
            'text'      => $this->language->get('text_home'),
            'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
            'separator' => false
        );

        $this->data['breadcrumbs'][] = array(
            'text'      => $this->language->get('heading_title'),
            'href'      => $this->url->link('configure/option', 'token=' . $this->session->data['token'] . $url, 'SSL'),
            'separator' => ' :: '
        );

        if (!isset($this->request->get['configureoption_id'])) {
            $this->data['action'] = $this->url->link('configure/option/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
        } else {
            $this->data['action'] = $this->url->link('configure/option/update', 'token=' . $this->session->data['token'] . '&configureoption_id=' . $this->request->get['configureoption_id'] . $url, 'SSL');
        }

        $this->data['cancel'] = $this->url->link('configure/option', 'token=' . $this->session->data['token'] . $url, 'SSL');

        if (isset($this->request->get['configureoption_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
            $option_info = $this->model_configure_option->getOption($this->request->get['configureoption_id']);
        }

        $this->data['token'] = $this->session->data['token'];

        $this->load->model('localisation/language');

        $this->data['languages'] = $this->model_localisation_language->getLanguages();

        if (isset($this->request->post['configureoption_description'])) {
            $this->data['configureoption_description'] = $this->request->post['configureoption_description'];
        } elseif (isset($this->request->get['configureoption_id'])) {
            $this->data['configureoption_description'] = $this->model_configure_option->getOptionDescriptions($this->request->get['configureoption_id']);
        } else {
            $this->data['configureoption_description'] = array();
        }



        if (isset($this->request->post['field_name'])) {
            $this->data['field_name'] = $this->request->post['field_name'];
        } elseif (!empty($option_info)) {
            $this->data['field_name'] = $option_info['field_name'];
        } else {
            $this->data['field_name'] = '';
        }

        if (isset($this->request->post['type'])) {
            $this->data['type'] = $this->request->post['type'];
        } elseif (!empty($option_info)) {
            $this->data['type'] = $option_info['type'];
        } else {
            $this->data['type'] = '';
        }

        if (isset($this->request->post['sort_order'])) {
            $this->data['sort_order'] = $this->request->post['sort_order'];
        } elseif (!empty($option_info)) {
            $this->data['sort_order'] = $option_info['sort_order'];
        } else {
            $this->data['sort_order'] = '';
        }

        if (isset($this->request->post['configureoption_value'])) {
            $option_values = $this->request->post['configureoption_value'];
        } elseif (isset($this->request->get['configureoption_id'])) {
            $option_values = $this->model_configure_option->getOptionValueDescriptions($this->request->get['configureoption_id']);
        } else {
            $option_values = array();
        }

        $this->load->model('tool/image');

        $this->data['configureoption_values'] = array();
        foreach ($option_values as $option_value) {
            $this->data['configureoption_values'][] = array(
                'configureoption_value_id'          => $option_value['configureoption_value_id'],
                'configureoption_value_description' => $option_value['configureoption_value_description'],
                'field_value'                    =>  $option_value['field_value'],
                'sort_order'               => $option_value['sort_order']
            );
        }

        $this->template = 'configure/option_form.tpl';
        $this->children = array(
            'common/header',
            'common/footer'
        );

        $this->response->setOutput($this->render());
    }

    protected function validateForm() {
        if (!$this->user->hasPermission('modify', 'configure/option')) {
            $this->error['warning'] = $this->language->get('error_permission');
        }

        foreach ($this->request->post['configureoption_description'] as $language_id => $value) {
            if ((utf8_strlen($value['name']) < 1) || (utf8_strlen($value['name']) > 128)) {
                $this->error['name'][$language_id] = $this->language->get('error_name');
            }
        }

        if (($this->request->post['type'] == 'select' || $this->request->post['type'] == 'radio' || $this->request->post['type'] == 'checkbox') && !isset($this->request->post['configureoption_value'])) {
            $this->error['warning'] = $this->language->get('error_type');
        }
        if (isset($this->request->post['configureoption_value'])) {
            foreach ($this->request->post['configureoption_value'] as $option_value_id => $option_value) {
                foreach ($option_value['configureoption_value_description'] as $language_id => $option_value_description) {
                    if ((utf8_strlen($option_value_description['name']) < 1) || (utf8_strlen($option_value_description['name']) > 128)) {
                        $this->error['configureoption_value'][$option_value_id][$language_id] = $this->language->get('error_option_value');
                    }
                }
            }
        }

        if (!$this->error) {
            return true;
        } else {
            return false;
        }
    }

    protected function validateDelete() {
        if (!$this->user->hasPermission('modify', 'configure/option')) {
            $this->error['warning'] = $this->language->get('error_permission');
        }

//        $this->load->model('configure/product');

//        foreach ($this->request->post['selected'] as $configureoption_id) {
//            $product_total = $this->model_configure_product->getTotalProductsByOptionId($configureoption_id);
//
//            if ($product_total) {
//                $this->error['warning'] = sprintf($this->language->get('error_product'), $product_total);
//            }
//        }

        if (!$this->error) {
            return true;
        } else {
            return false;
        }
    }

    public function autocomplete() {
        $json = array();

        if (isset($this->request->get['filter_name'])) {
            $this->language->load('configure/option');

            $this->load->model('configure/option');

            $this->load->model('tool/image');

            $data = array(
                'filter_name' => $this->request->get['filter_name'],
                'start'       => 0,
                'limit'       => 20
            );

            $options = $this->model_configure_option->getOptions($data);

            foreach ($options as $option) {
                $option_value_data = array();

                if ($option['type'] == 'select' || $option['type'] == 'radio' || $option['type'] == 'checkbox' || $option['type'] == 'image') {
                    $option_values = $this->model_configure_option->getOptionValues($option['configureoption_id']);

                    foreach ($option_values as $option_value) {
                        $option_value_data[] = array(
                            'configureoption_value_id' => $option_value['configureoption_value_id'],
                            'name'            => html_entity_decode($option_value['name'], ENT_QUOTES, 'UTF-8'),
                            'field_value'           => $option_value['field_value']
                        );
                    }

                    $sort_order = array();

                    foreach ($option_value_data as $key => $value) {
                        $sort_order[$key] = $value['name'];
                    }

                    array_multisort($sort_order, SORT_ASC, $option_value_data);
                }

                $type = '';

                if ($option['type'] == 'select' || $option['type'] == 'radio' || $option['type'] == 'checkbox' || $option['type'] == 'image') {
                    $type = $this->language->get('text_choose');
                }

                if ($option['type'] == 'text' || $option['type'] == 'textarea') {
                    $type = $this->language->get('text_input');
                }

                if ($option['type'] == 'file') {
                    $type = $this->language->get('text_file');
                }

                if ($option['type'] == 'date' || $option['type'] == 'datetime' || $option['type'] == 'time') {
                    $type = $this->language->get('text_date');
                }

                if ($option['type'] == 'password' ) {
                    $type = $this->language->get('text_password');
                }

                $json[] = array(
                    'configureoption_id'    => $option['configureoption_id'],
                    'name'         => strip_tags(html_entity_decode($option['name'], ENT_QUOTES, 'UTF-8')),
                    'category'     => $type,
                    'type'         => $option['type'],
                    'configureoption_value' => $option_value_data
                );
            }
        }

        $sort_order = array();

        foreach ($json as $key => $value) {
            $sort_order[$key] = $value['name'];
        }

        array_multisort($sort_order, SORT_ASC, $json);

        $this->response->setOutput(json_encode($json));
    }
}
?>